﻿
@{
    ViewBag.Title = "Index";
}
<style>
    #myModal {
        top: 150px;
    }
</style>

<div class="panel-body" style="padding-bottom:0px;">
    <div class="panel panel-default">
        <div class="panel-heading">查询条件</div>
        <div class="panel-body">
            <form id="formSearch" class="form-horizontal">
                <div class="form-group" style="margin-top:15px">
                    <label class="control-label col-sm-2" for="txtName">员工姓名:</label>
                    <div class="col-sm-3">
                        <input type="text" class="form-control" id="txtName">
                    </div>
                    <div class="col-sm-4" style="text-align:left;">
                        <button type="button" style="margin-left:50px" id="btn_query" class="btn btn-primary" onclick="QueryData()">查询</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div id="toolbar" class="btn-group">
        <input type="hidden" id="index" value="" />
        <button id="btn_add" type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal">
            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
        </button>
        <button id="btn_edit" type="button" class="btn btn-default" onclick="Update()">
            <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
        </button>
        <button id="btn_delete" type="button" class="btn btn-default" onclick="Delete()">
            <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
        </button>
    </div>


    <table id="tb1"></table>


    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title" id="myModalLabel">员工信息表数据操作</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">员工姓名:</label>
                            <input type="text" id="Name" placeholder="请输入" class="form-control">
                        </div>

                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">性别:</label>
                            <label class="radio-inline">
                                <input type="radio" name="Sex" id="boy" value="1" checked> 男
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="Sex" id="girl" value="2"> 女
                            </label>
                        </div>

                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">出生日期:</label>
                            <div class="input-group input-medium date-picker input-daterange" data-date-format="yyyy-mm-dd">
                                <input id="BirthDay" name="BirthDay" class="form-control" type="text" value="@Html.Raw(DateTime.Now.ToString("MM-dd-yyyy"))">

                            </div>
                        </div>

                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">角色:</label>
                            <div class="col-sm-5">
                                <select class="form-control m-b" name="account" id="RoleId">
                                    @foreach (var item in ViewBag.role)
                                    {
                                        <option value="@item.Id">@item.Name</option>
                                    }
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">籍贯:</label>
                            <input type="text" id="NativePlace" placeholder="请输入" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">现居住地</label>
                            <input type="text" id="Address" placeholder="请输入" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">联系电话:</label>
                            <input type="text" id="Tel" placeholder="请输入" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">邮箱:</label>
                            <input type="text" id="Email" placeholder="请输入" class="form-control">
                        </div>
                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">备注:</label>
                            <input type="text" id="Remark" placeholder="请输入" class="form-control">
                        </div>
                    </form>
                </div>
                <div class="modal-footer" style="text-align:center">
                    <button type="button" class="btn btn-default" data-dismiss="modal" id="close">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="Add()">提交</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>




</div>

<!-- 模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-hidden="true">
                    ×
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    数据操作
                </h4>
            </div>
            <div class="modal-body">
                按下 ESC 按钮退出。
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default"
                        data-dismiss="modal">
                    关闭
                </button>
                <button type="button" class="btn btn-primary">
                    提交更改
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


@section Scripts{
    <!-- Mainly scripts -->
    <script src="~/Content/Version/js/jquery-3.1.1.min.js"></script>

    <!-- Data picker -->
    <script src="~/Content/Version/js/plugins/datapicker/bootstrap-datepicker.js"></script>

    <link href="~/Content/bootstraptable/dist/bootstrap-table.css" rel="stylesheet" />
    <script src="~/Content/bootstraptable/dist/bootstrap-table.js"></script>
    <script src="~/Content/bootstraptable/dist/locale/bootstrap-table-zh-CN.min.js"></script>
    @*<script src="~/Scripts/jquery-3.4.1.js"></script>
        <script src="~/Scripts/jquery-3.4.1.min.js"></script>*@
    <script src="~/Content/layer/layer/layer.js"></script>
    <link href="~/Content/layer/layer/mobile/need/layer.css" rel="stylesheet" />
    <script>
        $(function () {
            ShowTable();
        });

        //日期控件显示
        var option = {
            language: "zh-CN",
            format: 'yyyy-mm-dd',
            autoclose: true,// 选中之后自动隐藏日期选择框
            clearBtn: true// 清除按钮
        }
        $('#BirthDay').datepicker(option);


        function QueryData() {
            //查询
            $('#tb1').bootstrapTable('refresh');
        }

        function ShowTable() {
            $('#tb1').bootstrapTable({
                url: '/Staff/ShowStaff',         //请求后台的URL（*）
                method: 'get',                      //请求方式（*）
                toolbar: '#toolbar',                //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                pagination: true,                   //是否显示分页（*）
                sortable: true,                     //是否启用排序
                sortOrder: "asc",                   //排序方式
                queryParams: function (params) {
                    params.name = $("#txtName").val();
                    return params;
                },
                sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                pageNumber: 1,                       //初始化加载第一页，默认第一页
                pageSize: 4,                       //每页的记录行数（*）
                pageList: [2, 25, 50, 100],        //可供选择的每页的行数（*）
                search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                strictSearch: true,
                showColumns: true,                  //是否显示所有的列
                showRefresh: false,                  //是否显示刷新按钮
                minimumCountColumns: 1,             //最少允许的列数
                clickToSelect: true,                //是否启用点击选中行
                //height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                uniqueId: "Id",                     //每一行的唯一标识，一般为主键列
                showToggle: false,                    //是否显示详细视图和列表视图的切换按钮
                cardView: false,                    //是否显示详细视图
                detailView: true,                   //是否显示父子表
                columns: [{
                    checkbox: true
                }, {
                    field: 'Name',
                    sortable: true,  //排序开启
                    title: '员工姓名'
                }, {
                    field: 'Sex',
                    title: '员工性别',
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return "男";
                        } else if (value == 2) {
                            return "女";
                        }
                    }
                },
                {
                    field: 'BirthDay',
                    title: '出生日期',
                    formatter: function (value, row, index) {
                        return changeDateFormat(value);
                    }
                },
                {
                    field: 'Address',
                    title: '现居住地'
                },
                {
                    field: 'Email',
                    title: '邮箱'
                },
                {
                    field: 'Tel',
                    title: '联系电话'
                },
                {
                    field: 'Remark',
                    title: '备注'
                }],
                onExpandRow: function (index, row, $detail) {
                    showSubTable(index, row, $detail);
                }
            });
        }

        $("#btn_add").click(function () {
            $("#index").val("");
            $("#Name").val("");
            $("#Email").val("");
            $("#Address").val("");
            $("#Tel").val("");
            $("#BirthDay").val("");
            $("#NativePlace").val();
        })



        //添加
        function Add() {
            $('input[name="Sex"]:checked').each(function (i, data) {
                var sex = $(this).val();
                if (sex == "1") {
                    $("#boy").attr("checked", true);
                }

                if (sex == "2") {
                    $("#girl").attr("checked", true);
                }
            })

            var str = "";
            if ($("#Name").val() == "") {
                str += "员工姓名不能为空\n";
            }
            if ($("#Address").val() == "") {
                str += "现居地址不能为空\n";
            }
            if ($("#Email").val() == "") {
                str += "邮箱不能为空\n";
            }
            if ($("#Tel").val() == "") {
                str += "联系电话不能为空\n";
            }
            if ($("#NativePlace").val() == "") {
                str += "籍贯不能为空\n";
            }
            if (str != "") {
                layer.msg(str);
                return false;
            }

            $.ajax({
                url: "/Staff/StaffAdd",
                data: {
                    Name: $("#Name").val(),
                    Address: $("#Address").val(),
                    Email: $("#Email").val(),
                    Tel: $("#Tel").val(),
                    Remark: $("#Remark").val(),
                    BirthDay: $("#BirthDay").val(),
                    RoleId: $("#RoleId").val(),
                    NativePlace: $("#NativePlace").val(),
                    index: $("#index").val(),
                    Sex: $('input[name="Sex"]:checked').val()
                },
                success: function (data) {
                    if (data) {
                        layer.msg("操作成功");
                        $("#close").click();//关闭弹窗
                        QueryData();//刷新
                    }
                    else {
                        layer.msg("操作失败");
                    }
                }
            })
        }


        //删除
        function Delete() {
            var data = $("#tb1").bootstrapTable('getSelections', function (row) {
                return row;
            })
            if (data == "") {
                layer.msg("请选择");
                return false;
            }
            var str = [];
            for (var i = 0; i < data.length; i++) {
                str.push(data[i].Id);
            }
            layer.msg('是否删除？', {
                time: 0 //不自动关闭
                , btn: ['确定', '取消']
                , yes: function (index) {
                    layer.close(index);

                    $.ajax({
                        url: "/Staff/StaffDelete"
                        , data: {
                            id: str.toString()
                        }
                        , success: function (res) {
                            if (res) {
                                layer.msg("删除成功");
                                //刷新页面
                                QueryData();
                            }
                            else {
                                layer.msg("删除失败");
                            }
                        }
                    })
                }
            });
        }

        //修改
        function Update() {
            var data = $("#tb1").bootstrapTable('getSelections', function (row) {
                return row;
            })
            if (data == "") {
                layer.msg("请选择");
                return false;
            } else if (data.length != 1) {
                layer.msg("只能选择一个");
                return false;
            }
            $("#btn_add").click();
            $("#index").val(data[0].Id);
            if ($("#index").val() != "") {
                $("#Name").val(data[0].Name);
                $("#Address").val(data[0].Address);
                $("#Email").val(data[0].Email);
                $("#Tel").val(data[0].Tel);
                $("#Remark").val(data[0].Remark);
                $("#BirthDay").val(changeDateFormat(data[0].BirthDay));
                $("#NativePlace").val(data[0].NativePlace);
                $("#RoleId").val(data[0].RoleId);
                $('input[name="Sex"]').eq(parseInt(data[0].Sex) - 1).prop("checked", true);
            }
        }




        function changeDateFormat(cellval) {
            var dateVal = cellval + "";
            if (cellval != null) {
                var date = new Date(parseInt(dateVal.replace("/Date(", "").replace(")/", ""), 10));
                var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
                return date.getFullYear() + "-" + month + "-" + currentDate + " ";
            }
        }
    </script>


}